﻿using System;
using System.Collections.Generic;

namespace Utilities.Configuration
{

    /**
     * Return instances and values registered at startup
     */
    public abstract class ApplicationContext : IApplicationContext
    {
        private static ApplicationContext instance = null;

        /// <summary>
        /// Gets instance
        /// </summary>
        public static ApplicationContext Instance
        {
            get
            {
                return instance;
            }

            set {
                instance = value;
            }
        }

        private Dictionary<string, string> lookupDictionary = new Dictionary<string, string>();

        public static void Init(ApplicationContext applicationContext)
        {
            Instance = applicationContext;


            string[][] data = applicationContext.GetBasicData();

            for (int i = 0; i < data.Length; i++)
            {
                Instance.lookupDictionary.Add(data[i][0], data[i][1]);
            }
        }

        protected abstract string[][] GetBasicData();

        public object GetInstance(Type interfaceType)
        {
            return GetInstance(interfaceType.Name);
        }

        public object GetInstance(string key)
        {
             Type t = Type.GetType(lookupDictionary[key]);

            return Activator.CreateInstance(t);
        }

        public string GetValue(string key)
        {
            return lookupDictionary[key];
        }
    }
}
